package com.atguigu.handler;

import com.atguigu.pojo.Movie;
import com.atguigu.service.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * 包名:com.atguigu.handler
 *
 * @author Leevi
 * 日期2021-09-06  10:31
 */
@Controller
@RequestMapping("/movie")
public class MovieHandler {
    @Autowired
    private MovieService movieService;

    @RequestMapping("/showList")
    public String showList(Model model){
        //1. 调用业务层的方法查询所有的movie
        List<Movie> movieList = movieService.getAll();
        //2. 将查询到的所有movie存储到请求域
        model.addAttribute("movieList",movieList);
        //3. 解析Thymeleaf模板显示所有movie
        return "list";
    }

    @RequestMapping("/removeMovie")
    public String removeMovie(@RequestParam("movieId") String movieId){
        //调用业务层的方法根据id删除movie
        movieService.removeMovieById(movieId);
        //重新查询所有
        return "redirect:/movie/showList";
    }

    @RequestMapping("/saveMovie")
    public String addMovie(Movie movie){
        //调用业务层的方法添加Movie
        movieService.saveMovie(movie);
        //重新查询所有
        return "redirect:/movie/showList";
    }

    @RequestMapping("/toUpdatePage")
    public String toUpdatePage(@RequestParam("movieId") String movieId,Model model){
        //1. 根据要修改的电影的id查询到当前电影的信息
        Movie movie = movieService.getMovieById(movieId);
        //2. 将当前电影的信息存储到请求域
        model.addAttribute("movie",movie);
        //3. 解析update页面
        return "update";
    }

    //
    @RequestMapping("/updateMovie")
    public String updateMovie(Movie movie){
        //1. 调用业务层的方法修改电影信息
        movieService.updateMovie(movie);
        //2. 重新查询所有
        return "redirect:/movie/showList";
    }
}
